using System.Collections;
using BCW;
using BCW.UI.Animation;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;

public class MarketButton : MonoBehaviour
{
	public UIWidget widget;

	public UIWidget holder;

	public UISprite background;

	public UILabel title;

	public UISprite icon;

	public UIBasicSprite texture;

	public UISprite selectedBG;

	public UISprite notSelectedBG;

	public UIAnimator uiAnimation;

	[HideInInspector]
	public MarketPanel parentPanel;

	public string id;

	private ResourceRequest resourceRequest;

	private ResourceRequest resourceRequestAlphaMask;

	public virtual void Awake()
	{
		texture = texture.ReplaceForCurrentPlatform();
		HOTween.Init();
	}

	public virtual void Init(string id, MarketPanel parentPanel)
	{
		this.parentPanel = parentPanel;
		this.id = id;
		if ((bool)texture)
		{
			texture.gameObject.SetActive(false);
		}
		if ((bool)title)
		{
			title.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Market/" + id + "_title"))) ? ScriptLocalization.Get("Market/" + id + "_title") : "Нет локализации");
			if (parentPanel is MarketCategoriesPanel)
			{
				if (id == "Best")
				{
					title.color = new Color(0.9843f, 0.8196f, 0.4f);
					background.spriteName = "ButtonShop_Category";
				}
				else
				{
					title.color = new Color(1f, 1f, 0.8f);
					background.spriteName = "ButtonShop_Category_2";
				}
			}
		}
		Select(parentPanel.selected.Equals(id));
	}

	public virtual void OnClick()
	{
		Select(true);
	}

	public virtual void Select(bool selected)
	{
		if (selected)
		{
			if (parentPanel.selectedButton != this)
			{
				if ((bool)parentPanel.selectedButton)
				{
					parentPanel.selectedButton.Select(false);
				}
				parentPanel.selectedButton = this;
				if ((bool)notSelectedBG)
				{
					notSelectedBG.alpha = 0f;
				}
				selectedBG.alpha = 1f;
			}
		}
		else
		{
			if ((bool)notSelectedBG)
			{
				notSelectedBG.alpha = 1f;
			}
			selectedBG.alpha = 0f;
		}
	}

	public virtual void SetIcon()
	{
		icon.spriteName = id;
	}

	private IEnumerator LoadProductTextureCoroutine(Product product)
	{
		ProductVehicleSkin skinProduct = null;
		if (product is ProductCar)
		{
			skinProduct = MarketController.Instance.GetEquipedVehicleSkin(product.id);
		}
		if (skinProduct != null)
		{
			yield return StartCoroutine(texture.LoadTextureAndAlphaMaskAsync("AtlasShopGuns/", "ASG_" + skinProduct.skinTextureName));
		}
		else if (product is ProductVehicleUpgrade || product is ProductFreeUpgrade)
		{
			yield return StartCoroutine(texture.LoadTextureAndAlphaMaskAsync("AtlasShopGuns/Upgrades/", product.texture.Replace("Upgrades/", string.Empty)));
		}
		else
		{
			yield return StartCoroutine(texture.LoadTextureAndAlphaMaskAsync("AtlasShopGuns/", product.texture));
		}
		if (texture.mainTexture == null)
		{
			yield return CoroutineRunner.Instance.StartCoroutine(texture.LoadTextureAndAlphaMaskAsync("AtlasShopGuns/", product.texture));
		}
		texture.gameObject.SetActive(true);
		if ((bool)icon)
		{
			icon.gameObject.SetActive(false);
		}
	}

	public virtual void LoadProductTexture(Product product)
	{
		if (product != null)
		{
			CoroutineRunner.Instance.StartCoroutine(LoadProductTextureCoroutine(product));
		}
	}

	public void UnloadProductTexture()
	{
		if (resourceRequest != null && resourceRequest.asset != null)
		{
			texture.mainTexture = null;
		}
	}
}
